package com.locationtracker.plus.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.gyf.immersionbar.ktx.immersionBar
import com.locationtracker.plus.R
import com.locationtracker.plus.app.AppWithAd
import com.locationtracker.plus.app.base.BaseActivity
import com.locationtracker.plus.app.eventViewModel
import com.locationtracker.plus.app.util.CacheUtil
import com.locationtracker.plus.app.util.CommonUtil.countDownCoroutines
import com.locationtracker.plus.app.weight.banner.WelcomeBannerAdapter
import com.locationtracker.plus.app.weight.banner.WelcomeBannerViewHolder
import com.locationtracker.plus.data.model.bean.WelcomeBean
import com.locationtracker.plus.databinding.ActivityWelcomeBinding
import com.zhpan.bannerview.BannerViewPager
import kotlinx.coroutines.Job
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.util.loge
import me.hgj.jetpackmvvm.ext.view.gone
import me.hgj.jetpackmvvm.ext.view.visible

@Suppress("DEPRECATED_IDENTITY_EQUALS")
class WelcomeActivity : BaseActivity<BaseViewModel, ActivityWelcomeBinding>() {

    private var resList = arrayOf(
        WelcomeBean(R.mipmap.yindaoye_1_photo, R.mipmap.ic_wel_1),
        WelcomeBean(R.mipmap.yindaoye_2_photo, R.mipmap.ic_wel_2),
        WelcomeBean(R.mipmap.yindaoye_3_photo, R.mipmap.ic_wel_3)
    )

    private var mCountdownJob: Job? = null

    private lateinit var mViewPager: BannerViewPager<WelcomeBean, WelcomeBannerViewHolder>

    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            fitsSystemWindows(false)
            statusBarDarkFont(!CacheUtil.isFirst())
        }
        //防止出现按Home键回到桌面时，再次点击重新进入该界面bug
        if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT !== 0) {
            finish()
            return
        }

        mDatabind.click = ProxyClick()
        mViewPager = findViewById(R.id.bannerView)
        if (CacheUtil.isFirst()) {
            //是第一次打开App 显示引导页
            mDatabind.welcomeBg.visible()
            mDatabind.welcomeJoin.visible()
            mDatabind.welcomeImage.gone()
            mViewPager.apply {
                setUserInputEnabled(false)
                setIndicatorVisibility(View.GONE)
                adapter = WelcomeBannerAdapter()
                setLifecycleRegistry(lifecycle)
                create(resList.toList())
            }
        } else {
            mCountdownJob = countDownCoroutines(5, lifecycleScope, {
                if (it == 0) jumpMain()
            })
        }
    }

    override fun createObserver() {
        eventViewModel.adLoadedEvent.observeInActivity(this) {
            mCountdownJob?.cancel() //取消倒计时
            val application = application as? AppWithAd
            // If the application is not an instance of MyApplication, log an error message and
            // start the MainActivity without showing the app open ad.
            if (application == null) {
                "Failed to cast application to App.".loge()
                jumpMain()
                return@observeInActivity
            }

            // Show the app open ad.
            application.showAdIfAvailable(this, object : AppWithAd.OnShowAdCompleteListener {
                override fun onShowAdComplete() {
                    jumpMain()
                }
            })
        }
    }

    fun jumpMain() {
        startActivity(Intent(this, MainActivity::class.java))
        finish()
        //带点渐变动画
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
    }

    inner class ProxyClick {
        fun next() {
            if (mViewPager.currentItem != mViewPager.data.size - 1) {
                mViewPager.setCurrentItem(mViewPager.currentItem + 1, true)
            } else {
                CacheUtil.setFirst(false)
                jumpMain()
            }
        }
    }
}